home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / Why Java Sucks / Newton11 / Newton.java < prev    next >
Encoding:
Text File  |  1998-06-13  |  9.1 KB  |  488 lines  |  [TEXT/R*ch]

  1. /*
  2.     Newton.java - A gravity simulation applet.
  3.  
  4.     Copyright (C) 1996-1997 by Michael J. Webb
  5. */
  6.  
  7. // Imports.
  8.  
  9. import java.applet.Applet;
  10.  
  11. import java.awt.*;
  12. import java.awt.event.*;
  13.  
  14. import java.lang.Math;
  15. import java.lang.Thread;
  16.  
  17. import java.util.Random;
  18.  
  19. import IdleTask;
  20. import TickerThread;
  21.  
  22. /** A gravity simulation applet.
  23.  */
  24.  
  25. public class Newton extends Applet
  26.     implements IdleTask
  27. {
  28.  
  29.     /** Applet info.
  30.      */
  31.     public String getAppletInfo()
  32.     {
  33.         return ("Newton 0.0d0, Copyright (C) 1996-1997 by Mike Webb");
  34.     }
  35.  
  36.     /** Applet parameter info.
  37.      */
  38.     public String[][] getParameterInfo()
  39.     {
  40.         String[][] theInfo =
  41.         {
  42.             {    pBodies,        "int",        "number of initial bodies"            },
  43.             {    pRockColor,        "Color",    "display color of bodies"            },
  44.         };
  45.         return (theInfo);
  46.     }
  47.  
  48.     /** Initialize the graphics elements of the gravity simulation.
  49.      */
  50.     public void init()
  51.     {
  52.         fActionAdapter = new ActionAdapter();
  53.         fItemAdapter = new ItemAdapter();
  54.  
  55.         GridBagLayout gridBagLayout;
  56.         gridBagLayout = new GridBagLayout();
  57.         setLayout(gridBagLayout);
  58.  
  59.         addNotify();
  60.         resize(getInsets().left + getInsets().right + 558,getInsets().top + getInsets().bottom + 441);
  61.  
  62.         panel1 = new java.awt.Panel();
  63.         panel1.setLayout(null);
  64.         panel1.setBounds(getInsets().left + 0,getInsets().top + 0,125,441);
  65.         panel1.setBackground(new Color(12632256));
  66.  
  67.         GridBagConstraints gbc;
  68.         gbc = new GridBagConstraints();
  69.         gbc.gridx = 0;
  70.         gbc.gridy = 0;
  71.         gbc.weighty = 1;
  72.         gbc.anchor = GridBagConstraints.NORTHWEST;
  73.         gbc.fill = GridBagConstraints.VERTICAL;
  74.         gbc.insets = new Insets(0,0,0,0);
  75.  
  76.         gridBagLayout.setConstraints(panel1, gbc);
  77.  
  78.         add(panel1);
  79.  
  80.         button3 = new java.awt.Button("Start");
  81.         button3.setBounds(10,10,80,23);
  82.         button3.setBackground(new Color(16777215));
  83.         panel1.add(button3);
  84.         button3.addActionListener(fActionAdapter);
  85.  
  86.         button4 = new java.awt.Button("Reseed");
  87.         button4.setBounds(10,70,80,23);
  88.         button4.setBackground(new Color(16777215));
  89.         panel1.add(button4);
  90.         button4.addActionListener(fActionAdapter);
  91.  
  92.         checkbox1 = new java.awt.Checkbox("Be Naughty");
  93.         checkbox1.setBounds(10,120,100,25);
  94.         panel1.add(checkbox1);
  95.         checkbox1.addItemListener(fItemAdapter);
  96.  
  97.         label1 = new java.awt.Label("Frames:");
  98.         label1.setBounds(10,165,50,15);
  99.         label1.setFont(new Font("Dialog", Font.PLAIN, 10));
  100.         panel1.add(label1);
  101.  
  102.         textField1 = new java.awt.TextField();
  103.         textField1.setEditable(false);
  104.         textField1.setText("0");
  105.         textField1.setBounds(60,165,50,16);
  106.         textField1.setFont(new Font("Courier", Font.PLAIN, 10));
  107.         panel1.add(textField1);
  108.  
  109.         label2 = new java.awt.Label("Rate:");
  110.         label2.setBounds(10,180,50,15);
  111.         label2.setFont(new Font("Dialog", Font.PLAIN, 10));
  112.         panel1.add(label2);
  113.  
  114.         textField2 = new java.awt.TextField();
  115.         textField2.setEditable(false);
  116.         textField2.setText("0");
  117.         textField2.setBounds(60,180,50,16);
  118.         textField2.setFont(new Font("Courier", Font.PLAIN, 10));
  119.         panel1.add(textField2);
  120.  
  121.         button5 = new java.awt.Button("Stop");
  122.         button5.setBounds(10,40,80,23);
  123.         button5.setBackground(new Color(16777215));
  124.         panel1.add(button5);
  125.         button5.addActionListener(fActionAdapter);
  126.  
  127.         newtonPanel1 = new java.awt.Panel();
  128.         newtonPanel1.setLayout(new BorderLayout(0,0));
  129.         newtonPanel1.setBounds(getInsets().left + 125, getInsets().top + 0, 433, 441);
  130.         newtonPanel1.setForeground(new Color(16777215));
  131.         newtonPanel1.setBackground(new Color(0));
  132.  
  133.         gbc = new GridBagConstraints();
  134.         gbc.gridx = 2;
  135.         gbc.gridy = 0;
  136.         gbc.weightx = 1;
  137.         gbc.weighty = 1;
  138.         gbc.anchor = GridBagConstraints.NORTHEAST;
  139.         gbc.fill = GridBagConstraints.BOTH;
  140.         gbc.insets = new Insets(0,0,0,0);
  141.         gridBagLayout.setConstraints(newtonPanel1, gbc);
  142.  
  143.         add(newtonPanel1);
  144.  
  145.         newtonCanvas1 = new NewtonCanvas();
  146.         newtonCanvas1.setBounds(0,41,433,400);
  147.         newtonCanvas1.setForeground(new Color(16777215));
  148.         newtonCanvas1.setBackground(new Color(0));
  149.         newtonPanel1.add("Center", newtonCanvas1);
  150.     }
  151.  
  152.     /** Start the applet.
  153.      */
  154.     public void start()
  155.     {
  156.         startTask();
  157.     }
  158.  
  159.     /** Stop the applet.
  160.      */
  161.     public void stop()
  162.     {
  163.         stopTask();
  164.         super.stop();
  165.     }
  166.  
  167.     public void destroy()
  168.     {
  169.         stopTask();
  170.         super.destroy();
  171.     }
  172.  
  173.     /** Start the simulation.
  174.      */
  175.     public synchronized void startTask()
  176.     {
  177.         if (fTicker == null)
  178.         {
  179.             fTicker = new TickerThread(this, 100);
  180.             fTicker.start();
  181.         }
  182.  
  183.         newtonCanvas1.startTask();
  184.     }
  185.  
  186.     /** Stop the simulation.
  187.      */
  188.     public synchronized void stopTask()
  189.     {
  190.         if (fTicker != null)
  191.         {
  192.             fTicker.stop();
  193.             fTicker = null;
  194.         }
  195.  
  196.         newtonCanvas1.stopTask();
  197.     }
  198.  
  199.     /** Periodically update the frame count and frame rate displays.
  200.      */
  201.     public synchronized void idle()
  202.     {
  203.         if (fTicker == null)
  204.         {
  205.             return;
  206.         }
  207.  
  208.         long frameCount = 0;
  209.  
  210.         frameCount = newtonCanvas1.getFrames();
  211.  
  212.         if (fOldFrames != frameCount)
  213.         {
  214.             double frameRate = newtonCanvas1.getFrameRate();
  215.             String rateStr = Double.toString(frameRate);
  216.             if (rateStr.length() > 5)
  217.             {
  218.                 rateStr = rateStr.substring(0,5);
  219.             }
  220.             textField1.setText(Long.toString(frameCount));
  221.             textField2.setText(rateStr);
  222.         }
  223.  
  224.         fOldFrames = frameCount;
  225.     }
  226.  
  227. /*
  228.         if (event.id == Event.WINDOW_DESTROY)
  229.         {
  230.             stopTask();
  231.         }
  232. */
  233.  
  234.     /** Put a random population of rocks in the simulation.
  235.      */
  236.     public void reseed()
  237.     {
  238.         newtonCanvas1.reseed();
  239.     }
  240.  
  241.     /** Handle Start actions.
  242.      */
  243.     public void StartAction(ActionEvent event)
  244.     {
  245.         newtonCanvas1.resume();
  246.     }
  247.  
  248.     /** Handle Stop actions.
  249.      */
  250.     public void StopAction(ActionEvent event)
  251.     {
  252.         newtonCanvas1.pause();
  253.     }
  254.  
  255.     /** Handle reseed actions.
  256.      */
  257.     public void ReseedAction(ActionEvent event)
  258.     {
  259.         reseed();
  260.     }
  261.  
  262.     /** Handle naughty actions.
  263.      */
  264.     public void NaughtyAction(ItemEvent event)
  265.     {
  266.         boolean naughty = checkbox1.getState();
  267.         newtonCanvas1.setNaughty(naughty);
  268.     }
  269.  
  270. // Private classes.
  271.  
  272.     private class ActionAdapter implements ActionListener
  273.     {
  274.         /** Action event handler.
  275.          */
  276.  
  277.         public void actionPerformed(ActionEvent event)
  278.         {
  279.             if (event.getSource() == button3)
  280.             {
  281.                 button3_Clicked(event);
  282.             }
  283.             else if (event.getSource() == button4)
  284.             {
  285.                 button4_Clicked(event);
  286.             }
  287.             else if (event.getSource() == button5)
  288.             {
  289.                 button5_Clicked(event);
  290.             }
  291.         }
  292.     }
  293.  
  294.     private class ItemAdapter implements ItemListener
  295.     {
  296.         /** Item stat changed (checkbox) event handler.
  297.          */
  298.  
  299.         public void itemStateChanged(ItemEvent event)
  300.         {
  301.             if (event.getSource() == checkbox1)
  302.             {
  303.                 checkbox1_Action(event);
  304.             }
  305.         }
  306.     }
  307.  
  308. // Private static variables.
  309.  
  310.     // Names of parameters.
  311.  
  312.     private final static String pBodies = "NumBodies";
  313.     private final static String pRockColor = "RockColor";
  314.  
  315. // Private instance variables.
  316.  
  317.     //{{DECLARE_CONTROLS
  318.     java.awt.Panel panel1;
  319.     java.awt.Button button1;
  320.     java.awt.Button button2;
  321.     java.awt.Button button3;
  322.     java.awt.Button button4;
  323.     java.awt.Checkbox checkbox1;
  324.     java.awt.Label label1;
  325.     java.awt.TextField textField1;
  326.     java.awt.Label label2;
  327.     java.awt.TextField textField2;
  328.     java.awt.Button button5;
  329.     java.awt.Panel newtonPanel1;
  330.     NewtonCanvas newtonCanvas1;
  331.     //}}
  332.  
  333.     TickerThread    fTicker = null;
  334.     long            fOldFrames = 0;
  335.  
  336.     ActionAdapter    fActionAdapter = null;
  337.     ItemAdapter        fItemAdapter = null;
  338.  
  339. // Private methods.
  340.  
  341.     /** Start button event handler.
  342.      */
  343.     void button3_Clicked(ActionEvent event)
  344.     {
  345.         StartAction(event);
  346.     }
  347.  
  348.     /** Reseed button event handler.
  349.      */
  350.     void button4_Clicked(ActionEvent event)
  351.     {
  352.         ReseedAction(event);
  353.     }    
  354.  
  355.     /** Naughty button event handler.
  356.      */
  357.     void checkbox1_Action(ItemEvent event)
  358.     {
  359.         NaughtyAction(event);
  360.     }
  361.  
  362.     /** Stop button event handler.
  363.      */
  364.     void button5_Clicked(ActionEvent event)
  365.     {
  366.         StopAction(event);
  367.     }
  368.  
  369.     /** Utility method to fetch an int from a parameter.
  370.      */
  371.     private int fetchInt(String pName, int defaultValue)
  372.     {
  373.         String spec;
  374.         int value;
  375.  
  376.         spec = getParameter(pName);
  377.  
  378.         if (spec != null)
  379.         {
  380.             try
  381.             {
  382.                 value = Integer.parseInt(spec);
  383.             }
  384.             catch (NumberFormatException e)
  385.             {
  386.                 value = defaultValue;
  387.             }
  388.         }
  389.         else
  390.         {
  391.             value = defaultValue;
  392.         }
  393.  
  394.         return (value);
  395.     }
  396.  
  397.     /** Utility method to fetch a double from a parameter.
  398.      */
  399.     private double fetchDouble(String pName, double defaultValue)
  400.     {
  401.         String spec;
  402.         double value;
  403.  
  404.         spec = getParameter(pName);
  405.  
  406.         if (spec != null)
  407.         {
  408.             try
  409.             {
  410.                 value = (Double.valueOf(spec)).doubleValue();
  411.             }
  412.             catch (NumberFormatException e)
  413.             {
  414.                 value = defaultValue;
  415.             }
  416.         }
  417.         else
  418.         {
  419.             value = defaultValue;
  420.         }
  421.  
  422.         return (value);
  423.     }
  424.  
  425.     /** Utility method to fetch a String from a parameter.
  426.      */
  427.     private String fetchString(String pName, String defaultValue)
  428.     {
  429.         String spec;
  430.         String value;
  431.  
  432.         spec = getParameter(pName);
  433.  
  434.         if (spec != null)
  435.         {
  436.             value = spec;
  437.         }
  438.         else
  439.         {
  440.             value = defaultValue;
  441.         }
  442.  
  443.         return (value);
  444.     }
  445.  
  446.     /** Utility method to fetch a Color from a parameter.
  447.      */
  448.     private Color fetchColor(String pName, Color defaultValue)
  449.     {
  450.         String spec;
  451.         Color value;
  452.  
  453.         spec = getParameter(pName);
  454.  
  455.         if (spec != null)
  456.         {
  457.             try
  458.             {
  459.                 int r = Integer.parseInt(spec.substring(0,2));
  460.                 int g = Integer.parseInt(spec.substring(2,4));
  461.                 int b = Integer.parseInt(spec.substring(4,6));
  462.                 value =
  463.                     new Color
  464.                     (
  465.                         (float)((double)r / 99.0),
  466.                         (float)((double)g / 99.0),
  467.                         (float)((double)b / 99.0)
  468.                     );
  469.             }
  470.             catch (NumberFormatException e)
  471.             {
  472.                 value = defaultValue;
  473.             }
  474.             catch (StringIndexOutOfBoundsException e)
  475.             {
  476.                 value = defaultValue;
  477.             }
  478.         }
  479.         else
  480.         {
  481.             value = defaultValue;
  482.         }
  483.  
  484.         return (value);
  485.     }
  486.  
  487. }
  488.